<?php namespace App\Http\Controllers\Site;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Cache;

use App\Job;
use App\Category;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class IndexController extends Controller
{
    protected function index_products(){
        $products = DB::select('SELECT  *  FROM t_job main WHERE (SELECT COUNT(1) FROM t_job sub WHERE main.parentcategory = sub.parentcategory AND main.jobid > sub.jobid) < 6 AND state = "已发布"  ORDER BY parentcategory');
        $navs = Category::where('parentid',0)->lists('name','categoryid');
        foreach($navs as $key=>$nav){
            $index[$key]['name'] = $nav;
            $index[$key]['data'] = [];
            foreach($products as $product){
                if($key == $product->parentcategory){
                    array_push($index[$key]['data'],$product);
                } else {
                    continue;
                }
            }
        }
        return $index;
    }

    //显示内容
    public function index(){
        $index_products = $this->index_products();
        return view('site.index', [
            'index_products' => $index_products,
        ]);
    }

/*
    //装修
    public  function getDecorate(Request $request){
        $cate = $request['cate'];
        $decorate = Job::where('kind', '快工')

            ->orderBy('pubtime', 'DESC');
//        if($cate){
//            $decorate = $decorate->where('categoryid', '=', $cate)
//        }
        $kind = Cache::get(config('site.cachenames.kind'));
        $subkind = array_filter($kind, function($item){
            return $item->categoryid == 5;
        });
//        print_r( current( $subkind )->subkind);
//        return;
        $decorate = $decorate->paginate(config('site.pageSize'));
        return view('site.decorate.decorate',[
            'pageName'  =>'service',
            'decorate'  => $decorate,
            'subkind'   => current( $subkind )->subkind,
            'params'    => ['cate'=>$cate]
        ]);
    }

    //装修详情
    public function getDecoratedetail(Request $request){
        $decoreate = Job::where('jobid', $request['jobid'])
            ->select('describe', 'imgurl')
            ->first();
        return view('site.decorate.decoratedetail',[
            'decorate'      => $decoreate
        ]);
    }*/

    /*
    public  function getCostume(){
        return 1;
        return view('');
    }*/

    public function getAbout(){
        return view('site.about', [
            'pageName' => 'about'
        ]);
    }

    public function getJoinus(){
        return view('site.joinus',[
            'pageName' => ''
        ]);
    }

    //
    public function getContact(){
        return view('site.contact',[
            'pageName'  => 'contact'
        ]);
    }
}
